home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / info-service / wais / ir-book-sources / stemmer / stemmer.c < prev    next >
C/C++ Source or Header  |  1993-04-08  |  4KB  |  126 lines

  1.  
  2. /*******************************   stemmer.c   ********************************
  3.  *
  4.  *  Program to demonstrate and test the Porter stemming function.  This 
  5.  *  program takes a single filename on the command line and lists stemmed
  6.  *  terms on stdout.
  7.  *
  8. **/
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12.  
  13. #include "stem.h"
  14.  
  15. /******************************************************************************/
  16. /********************   Private Defines and Data Structures   *****************/
  17.  
  18. #define EOS                           '\0'
  19.  
  20. /******************************************************************************/
  21. /************************   Private Function Definitions   ********************/
  22.  
  23. #ifdef __STDC__
  24.  
  25. static char * GetNextTerm( FILE *stream, int size, char *term );
  26.  
  27. #else
  28.  
  29. static char * GetNextTerm();
  30.  
  31. #endif
  32.  
  33. /*FN***************************************************************************
  34.  
  35.         GetNextTerm( stream, size, term )
  36.  
  37.    Returns: char * -- buffer with the next input term, NULL at EOF
  38.  
  39.    Purpose: Grab the next token from an input stream
  40.  
  41.    Plan:    Part 1: Return NULL immediately if there is no input
  42.             Part 2: Initialize the local variables
  43.             Part 3: Main Loop: Put the next word into the term buffer
  44.             Part 4: Return the output buffer
  45.  
  46.    Notes:   None.
  47. **/
  48.  
  49. static char *
  50. GetNextTerm( stream, size, term )
  51.    FILE *stream;  /* in: source of input characters */
  52.    int size;      /* in: bytes in the output buffer */
  53.    char *term;    /* in/out: where the next term in placed */
  54.    {
  55.    char *ptr;  /* for scanning through the term buffer */
  56.    int ch;     /* current character during input scan */
  57.  
  58.            /* Part 1: Return NULL immediately if there is no input */
  59.    if ( EOF == (ch = getc(stream)) ) return( NULL );
  60.  
  61.                   /* Part 2: Initialize the local variables */
  62.    *term = EOS;
  63.    ptr = term;
  64.  
  65.         /* Part 3: Main Loop: Put the next word into the term buffer */
  66.    do
  67.       {
  68.          /* scan past any leading non-alphabetic characters */
  69.       while ( (EOF != ch ) && !isalpha(ch) ) ch = getc( stream );
  70.  
  71.          /* copy input to output while reading alphabetic characters */
  72.       while ( (EOF != ch ) && isalpha(ch) )
  73.          {
  74.          if ( ptr == (term+size-1) ) ptr = term;
  75.          *ptr++ = ch;
  76.          ch = getc( stream );
  77.          }
  78.  
  79.          /* terminate the output buffer */
  80.       *ptr = EOS;
  81.       }
  82.    while ( (EOF != ch) && !*term );
  83.  
  84.                     /* Part 4: Return the output buffer */
  85.    return( term );
  86.  
  87.    } /* GetNextTerm */
  88.  
  89. /******************************************************************************/
  90. /*FN***************************************************************************
  91.  
  92.         main( argc, argv )
  93.  
  94.    Returns: int -- 0 on success, 1 on failure
  95.  
  96.    Purpose: Program main function
  97.  
  98.    Plan:    Part 1: Open the input file
  99.             Part 2: Process each word in the file
  100.             Part 3: Close the input file and return
  101.  
  102.    Notes:   
  103. **/
  104.  
  105. int
  106. main( argc, argv )
  107.    int argc;     /* in: how many arguments */
  108.    char *argv[]; /* in: text of the arguments */
  109.    {
  110.    char term[64];   /* for the next term from the input line */
  111.    FILE *stream;    /* where to read characters from */
  112.  
  113.                        /* Part 1: Open the input file */
  114.    if ( !(stream = fopen(argv[1],"r")) ) exit(1);
  115.  
  116.                   /* Part 2: Process each word in the file */
  117.    while( GetNextTerm(stream,64,term) )
  118.       if ( Stem(term) ) (void)printf( "%s\n", term );
  119.  
  120.                  /* Part 3: Close the input file and return */
  121.    (void)fclose( stream );
  122.    return(0);
  123.  
  124.    } /* main */
  125.  
  126.